
/**
 * @function: add new person
 *
 */

function getAllComments() {

    $.ajax({
        url:'/01_jspBasics/test/ajax',
        type:'GET',
        datatype:'json',
        data: {
            'action':"getComments"
        },
        success:function(res) {
            console.log(res) ;
            var $table = $('.tb-table') ;
            var $tbody = $('#tb-content') ;
            var $html = "" ;
            /**
             * $().each(function(index, item) {
             *     $(this)
             * }
             */
            $.each(res.comments, function(index, item) {

               $html += "<tr>"
                $html += "<td>" + index + "</td>"
                $html += "<td>" + item.candidateName + "</td>"
                $html += "<td>" + item.userName + "</td>"
                $html += "<td>" + item.commTime + "</td>"
                $html += "<td>" + item.comment + "</td>"
                $html += "</tr>"
            }) ;
            $tbody.html($html) ;
        },
        error: function(res) {
            console.log("failed: " + res) ;
        }
    })
}

/**
 * @function: add new comment
 */
function addComment() {

    var candidateName = $('.add-form input[name="candidateName"]').val() ;
    var userName = $('.add-form input[name="userName"]').val() ;
    var comment = $('.add-form textarea[name="comment"]').val() ;

    var formData = {
        'candidateName':candidateName,
        'userName': userName,
        'comment':comment,
        'action':"addComment"
    } ;
    $.ajax({
        url:"/01_jspBasics/test/ajax",
        type:"POST",
        dataType:"json",
        data:JSON.stringify(formData),
        success:function(resp) {
            alert("添加成功。") ;
            $('.cover').hide() ;
            location.reload(true) ;
        },
    })


}
